﻿<template>
    <el-container>
        <el-header>
            <div class="left-panel">
				<sc-export 
                    :queryType="`warehouse-stock/export`" 
                    :queryList="queryList" 
                    :fileName="'实时库存'"
                    :isTest="true" 
                    showText="导出"/>
			</div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon
                        class="vab-dropdown"
                        :class="{ 'vab-dropdown-active': fold }"
                        icon="arrow-up-s-line"
                    />
                </el-button>
            </div>
        </el-header>
        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form inline label-width="49px" :model="queryForm" style="margin-top: 5px">
                    <el-form-item label="仓库名称" label-width="100px">
                        <sc-warehouse-select
                            ref="orgselect"
                            :props="warehouseProps"
                            :selectKeyValue="'warehouseId'"
                            v-model="queryList.warehouses"
                            @fatherMethod="warehouseSelectProcess"
                        ></sc-warehouse-select>
                    </el-form-item>
                    <el-form-item label="物料编码" label-width="100px">
                        <el-input
                            placeholder="---请输入编码---"
                            v-model="queryList.materialCode"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="物料名称" label-width="100px">
                        <el-input
                            placeholder="---请输入名称---"
                            v-model="queryList.materialName"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="物料规格" label-width="100px">
                        <el-input
                            placeholder="---请输入名称---"
                            v-model="queryList.materialSpecs"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="物料分类" label-width="100px">
                        <el-tree-select
                            placeholder="---请选择物料分类---"
                            v-model="queryList.materialTypeIds"
                            :props="{ label: 'label', value:'value',children: 'children' }"
                            :data="materialTypeTree" 
                            collapse-tags
                            collapse-tags-tooltip
                            multiple
                            :render-after-expand="false"
                            show-checkbox
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label-width="100px" label="库存单位">
                        <el-select
                            placeholder="---请选择单位---"
                            v-model="queryList.UnitCode"
                            style="width: 300px"
                        >
                            <el-option
                                v-for="item in optionsUnit"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="库存数量" label-width="100px">
                        <el-input
                            placeholder="---请输入最小库存---"
                            v-model="queryList.minStock"
                            style="width: 145px"
                        />
                        <span style="width: 10px;">-</span>
                        <el-input
                            placeholder="---请输入最大库存---"
                            v-model="queryList.maxStock"
                            style="width: 145px"
                        />
                    </el-form-item>
                    <el-form-item label="成本单价" label-width="100px">
                        <el-input
                            placeholder="---请输入最小成本单价---"
                            v-model="queryList.minPrice"
                            style="width: 145px"
                        />
                        <span style="width: 10px;">-</span>
                        <el-input
                            placeholder="---请输入最大成本单价---"
                            v-model="queryList.maxPrice"
                            style="width: 145px"
                        />
                    </el-form-item>
                    <el-form-item label="库存时间" label-width="100px">
                        <el-date-picker
                            style="width: 300px"
                            v-model="queryList.crDates"
                            type="datetimerange"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            format="YYYY-MM-DD HH:mm:ss"
                            range-separator="-"
                            unlink-panels="true"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                            :default-time="defaultTime"
                        />
                    </el-form-item>
                    <el-form-item>
                        <el-checkbox
                            label="过滤零库存物料"
                            :value="true"
                            v-model="queryList.isStock"
                            style="margin-right: 10px;"
                        />
                        <el-checkbox
                            label="过滤已禁用物料"
                            :value="true"
                            v-model="queryList.isMaterialDisable"
                            style="margin-right: 10px;"
                        />
                        <el-checkbox
                            label="过滤无出入库物料"
                            :value="true"
                            v-model="queryList.isInOrOutStock"
                            style="margin-right: 10px;"
                        />
                        <el-checkbox
                            label="仅显示无出入库的物料"
                            :value="true"
                            v-model="queryList.IsNotInOrOutStock"
                            style="margin-right: 10px;"
                        />
                    </el-form-item>
                </el-form>
            </vab-query-form-top-panel>
        </el-foot>

        <el-main class="nopadding">
            <scTable
                ref="mytable"
                :queryType="'warehouse-stock/pages'"
                :data="listData"
                :queryList="queryList"
                :column="columnList"
                :orderList="OrderList"
                :sort-orders="['ascending', 'descending']"
                @selection-change="selectionChange"
                row-key="warehouseId"
                key="warehouseId"
                stripe
                show-summary
                remoteSummary
            >
                <el-table-column type="selection" width="50"></el-table-column>
                <!-- <el-table-column prop="amount" label="仓库名称" width="170" sortable></el-table-column> -->
                <el-table-column prop="materialCode" label="物料编号" width="140" sortable></el-table-column>
                <el-table-column prop="materialName" label="物料名称" width="240" sortable></el-table-column>
                <el-table-column prop="materialSpecs" label="规格" width="160" sortable></el-table-column>
                <el-table-column prop="materialTypeName" label="物料分类" width="200" sortable></el-table-column>
                <el-table-column prop="unitName" label="单位" width="120" sortable></el-table-column>
                <el-table-column prop="stockQty" label="库存数量" width="170" sortable></el-table-column>
                <el-table-column prop="price" label="成本单价" width="170" sortable></el-table-column>
                <el-table-column prop="amount" label="库存金额" width="170" sortable></el-table-column>
                <!-- <el-table-column prop="warehouseName" label="最低预警" width="170" sortable></el-table-column> -->
                <!-- <el-table-column label="操作" fixed="right" align="right" width="230">
                    <template #default="scope">
                        <el-button-group>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="opShow(scope.row, scope.$index)"
                            >查看</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                :disabled="!editState"
                                @click="opEdit(scope.row, scope.$index)"
                            >编辑</el-button>
                            <el-popconfirm
                                title="确定删除吗？"
                                @confirm="opDelete(scope.row, scope.$index)"
                            >
                                <template #reference>
                                    <el-button
                                        text
                                        type="primary"
                                        size="small"
                                        :disabled="!deleteState"
                                    >删除</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-show="!scope.row.isStockSet"
                                @click="opStartStock(scope.row, scope.$index)"
                            >设置期初</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>-->
            </scTable>
        </el-main>
    </el-container>
</template>

<script>
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import config from "@/config";
export default {
    name: "warehouseStock",
    components: {
        publicjs,
    },
    data() {
        return {
            dialogSave: false,
            dialogStartStock: false,
            showCondition: false,
            selectionRows: [],
            pkList: ["warehouseId"],
            columnList: [],
            OrderList: { warehouseId: "Asc" },
            queryList: {
                warehouseCode: "",
                warehouseName: "",
                status: "",
            },
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            warehouseProps: {
                label: "warehouseName",
                value: "warehouseId",
            },
            orgCodeList: "",
            orgNameList: "",
            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
            optionsUnit: [],
            materialTypeTree:[],
            defaultTime:[
                new Date(2000, 1, 1, 0, 0, 0),
                new Date(2000, 2, 1, 23, 59, 59),
            ]
        };
    },
    methods: {
        //添加
        opAdd() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add");
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Edit").setData(row);
            });
        },
        //查看
        opShow(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Show").setData(row);
            });
        },
        //库存设置
        opStartStock(row) {
            this.dialogStartStock = true;
            this.$nextTick(() => {
                this.$refs.startStockDialog.open("Save").setData(row);
            });
        },
        //设置状态
        async setStatus(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    row.status == 0 ? "warehouse/disabled" : "warehouse/enable",
                    { warehouseIds: [row.warehouseId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //设置状态
        async setIsMinus(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    row.isMinus == 0 ? "warehouse/close" : "warehouse/open",
                    { warehouseIds: [row.warehouseId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //设置状态
        async setIsDefault(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "warehouse/default",
                    {
                        warehouseId: row.warehouseId,
                        warehouseType: row.warehouseType,
                        isDefault: row.isDefault == 1,
                    }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //删除
        async opDelete(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "warehouse/delete",
                    { warehouseIds: [row.warehouseId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //仓库
        async warehouseSelectProcess(rowData) {
            this.queryList.warehouses = [];
            this.queryList.warehouseIds = [];
            if (rowData && rowData.length > 0) {
                rowData.map((p, index) => {
                    if (p) {
                        this.queryList.warehouses = [
                            ...this.queryList.warehouses,
                            {
                                warehouseId: p.warehouseId,
                                warehouseName: p.warehouseName,
                            },
                        ];
                        this.queryList.warehouseIds = [
                            ...this.queryList.warehouseIds,
                            p.warehouseId,
                        ];
                    }
                });
            }
            await this.opBindData();
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "Add") {
                this.$refs.mytable.refresh();
            } else if (mode == "Edit") {
                this.$refs.mytable.refresh();
            }
        },
        async opBindData() {
            this.$refs.mytable.clearSort();
            this.$refs.mytable.getData();
        },
        resetSearch() {
            this.queryList = {};
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "material-type/tree",
                    { status: 1 }
                );
                if (returnValue.status == 200) {
                    this.materialTypeTree = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/dic-options",
                    { ParentBaseDictionaryCodes: ["001", "002"] }
                );
                if (returnValue.status == 200) {
                    this.optionsBrand = returnValue.response.find(
                        (p) => p.code == "001"
                    ).optionsDetails;
                    this.optionsUnit = returnValue.response.find(
                        (p) => p.code == "002"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        await this.initOptions();
        await this.opBindData();
    },
};
</script>
